Paging Memory Model

Paging splits physical memory into equal-sized blocks

Page Mapping

Frames can be anywhere in the real physical memory
But the virtual pages look like a contiguous address space
![[Pasted image 20250519051546.png]]

Page Table Lookups

![[Pasted image 20250519051759.png]]

Segmentation and Paging

Segmentation...

Advantages of Paging

Segmentation with Paging

The Intel x86 MMU supports segmentation with paging

CPU sends logical address to Segmentation Unit
Segmentation Unit sends virtual address to Paging Unit
Paging Unit sends physical address to MMU

Page Swapping

The OS can allocate more memory than is physically installed in the system

Page Replacement Problem

When a page is brought from disk to memory

OS will have a page replacement policy

The memory manager might get it wrong

Principle of Locality

The optimal policy is to swap out the page that won't be needed for the longest time

In other words, memory addresses that a process refers to in the next time period are likely to be close to the addresses used in the current period
Cannot always be guaranteed, so page faults are a normal part of how an OS works

Working Set

Following the principle of locality

Predicting the Working Set

Memory manager knows which pages have been used in which time periods

Page z a b a a a z b b a
Time 0 1 2 3 4 5 6 7 8 9
The working set W(3,3) here is from time 3 to time 6: az
By the principle of locality, the working set for the next time period is likely to be similar to the one for the current period
For each process, the memory manager

Working Set Accuracy

Accuracy of the working set depends on its size

Working set of a process will change as execution moves from one locality to another

Number of page faults will increase during this transition between localities (which is an expected part of how memory management works)

Page Size

Large pages require a smaller page table but increase the amount of wasted memory

Windows and Linux both use 4 KB pages by default

Cache Memory

Storage hierarchy decreases in speed (and cost) as we move away from the CPU

The cache is a small, fast memory area that sits between the CPU and main memory

Cache Hits and Misses

When the CPU requests data from memory, first it checks the cache
If the data is in the cache

Cache Levels and Latency

Modern processes are incredibly fast
Memory access is incredibly slow by comparison
CPU can execute hundreds of instructions in the time it takes to fetch a data block from main memory into the cache (latency)
Larger caches have better hit rates but longer latency

Modern CPUs have multiple levels of cache

Memory Hierarchy

From Expensive/Fast/Small to Cheap/Slow/Large